package io.renren.modules.hospital.controller;


import io.renren.common.utils.R;
import io.renren.modules.hospital.service.impl.UploadImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("hospital/img")
@Api(value = "图片上传接口", tags = "图片上传接口")
public class UploadImgController {

    @Autowired
    UploadImgService uploadImgService;

    @PostMapping("upload")
    @ApiOperation("上传图片")
    public R uploadImg(MultipartFile file) {
        String dirs = "/usr/share/nginx/html/img/";
        String imgName = UUID.randomUUID() + file.getOriginalFilename();
        try {
            file.transferTo(new File(dirs + imgName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.ok();
    }

    @PostMapping("uploadAliyun")
    @ApiOperation("上传图片至阿里云图床")
    public R uploadAliyunImg(@RequestPart("file") MultipartFile file) {
        String upload = uploadImgService.upload(file);

        return R.ok(upload);
    }


    @PostMapping("uploagAliyunImgs")
    @ApiOperation("上传多张图片至阿里云图床")
    public R uploagAliyunImgs(@RequestPart("files") List<MultipartFile> files) {
        StringBuilder buffer = new StringBuilder();
        for (MultipartFile file : files) {
            buffer.append(uploadImgService.upload(file)).append(";");
        }
        return R.ok(buffer.toString());
    }
}


